﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EBSystem.Data;
using EBSystem.Pages.UserPages;
using EBSystem.SQL;
using EBSystem.ViewModels;

namespace EBSystem.Pages
{
    /// <summary>
    /// UserSourcePage.xaml 的交互逻辑
    /// </summary>
    public partial class UserSourcePage : UserControl
    {
        int page = 1;
        int priceSort = 1;
        int nowGoodsIndex = 0;

        public UserSourcePage()
        {
            InitializeComponent();

            DataContext = new UserSourceViewModel();
        }

        private void updateUserAccountInfoButton_Click(object sender, RoutedEventArgs e)
        {
            UpdateUserAccountInfoWindow updateUserAccountInfoWindow = new();
            updateUserAccountInfoWindow.ShowDialog();
        }

        private void addDeliveryAddressButton_Click(object sender, RoutedEventArgs e)
        {
            AddDeliveryAddressWindow AddDeliveryAddressWindow = new();
            AddDeliveryAddressWindow.ShowDialog();
        }

        private void deleteDeliveryAddressButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteDeliveryAddressWindow deleteDeliveryAddressWindow = new();
            deleteDeliveryAddressWindow.ShowDialog();
            deliveryAddress.Children.Clear();
            InitializeData();
        }

        private void quitLoginButton_Click(object sender, RoutedEventArgs e)
        {
            Clear();
            UserData.Clear();
            MainWindow.GetThisMainWindow()?.GotoLoginPage();
        }

        public void Clear()
        {
            userNameTextBox.Text = "";
            nickNameTextBox.Text = "";
            emailTextBox.Text = "";
            phoneNumberTextBox.Text = "";

            deliveryAddress.Children.Clear();
            buyButton.IsEnabled = false;

            page = 1;
            nowGoodsIndex = 0;
        }

        public void InitializeData()
        {
            userNameTextBox.Text = UserData.userName;
            nickNameTextBox.Text = UserData.nickName;
            emailTextBox.Text = UserData.email;
            phoneNumberTextBox.Text = UserData.phoneNumber;

            foreach (List<string> temp in UserData.deliveryAddress)
            {
                TextBox textBox = new();
                textBox.Text = "";
                textBox.Margin = new(0, 10, 10, 10);
                textBox.IsReadOnly = true;
                textBox.TextWrapping = TextWrapping.WrapWithOverflow;
                foreach (string item in temp)
                {
                    textBox.Text += item;
                    textBox.Text += "  ";
                }
                deliveryAddress.Children.Add(textBox);
            }
        }

        private void searchGoodsButton_Click(object sender, RoutedEventArgs e)
        {
            SQLConnection.SearchGoods(searchGoodsTextBox.Text, page, priceSort);
            int n = UserData.goodsData.Rows.Count;
            for (int i = 0; i < n; i++)
            {
                (goods.Children[i] as Grid).Visibility = Visibility.Visible;
                Grid temp = (goods.Children[i] as Grid).Children[1] as Grid;
                (temp.Children[0] as TextBox).Text = UserData.goodsData.Rows[0]["goodsName"].ToString();
                (temp.Children[1] as TextBox).Text = UserData.goodsData.Rows[0]["goodsSpecification"].ToString();
                (temp.Children[2] as TextBox).Text = UserData.goodsData.Rows[0]["goodsPrice"].ToString();
            }
            for(int i = n; i < 5; i++)
            {
                (goods.Children[i] as Grid).Visibility = Visibility.Hidden;
            }
            buyButton.IsEnabled = false;
        }

        private void goods0_MouseEnter(object sender, MouseEventArgs e)
        {
            goodsName.Text = UserData.goodsData.Rows[0]["goodsName"].ToString();
            goodsSpecification.Text = UserData.goodsData.Rows[0]["goodsSpecification"].ToString();
            goodsPrice.Text = UserData.goodsData.Rows[0]["goodsPrice"].ToString();
            storeName.Text = UserData.goodsData.Rows[0]["storeName"].ToString();
            goodsInventory.Text = UserData.goodsData.Rows[0]["goodsInventory"].ToString();

            nowGoodsIndex = 0;
            buyButton.IsEnabled = true;
        }

        private void goods1_MouseEnter(object sender, MouseEventArgs e)
        {
            goodsName.Text = UserData.goodsData.Rows[1]["goodsName"].ToString();
            goodsSpecification.Text = UserData.goodsData.Rows[1]["goodsSpecification"].ToString();
            goodsPrice.Text = UserData.goodsData.Rows[1]["goodsPrice"].ToString();
            storeName.Text = UserData.goodsData.Rows[1]["storeName"].ToString();
            goodsInventory.Text = UserData.goodsData.Rows[1]["goodsInventory"].ToString();

            nowGoodsIndex = 1;
            buyButton.IsEnabled = true;
        }

        private void goods2_MouseEnter(object sender, MouseEventArgs e)
        {
            goodsName.Text = UserData.goodsData.Rows[2]["goodsName"].ToString();
            goodsSpecification.Text = UserData.goodsData.Rows[2]["goodsSpecification"].ToString();
            goodsPrice.Text = UserData.goodsData.Rows[2]["goodsPrice"].ToString();
            storeName.Text = UserData.goodsData.Rows[2]["storeName"].ToString();
            goodsInventory.Text = UserData.goodsData.Rows[2]["goodsInventory"].ToString();

            nowGoodsIndex = 2;
            buyButton.IsEnabled = true;
        }

        private void goods3_MouseEnter(object sender, MouseEventArgs e)
        {
            goodsName.Text = UserData.goodsData.Rows[3]["goodsName"].ToString();
            goodsSpecification.Text = UserData.goodsData.Rows[3]["goodsSpecification"].ToString();
            goodsPrice.Text = UserData.goodsData.Rows[3]["goodsPrice"].ToString();
            storeName.Text = UserData.goodsData.Rows[3]["storeName"].ToString();
            goodsInventory.Text = UserData.goodsData.Rows[3]["goodsInventory"].ToString();

            nowGoodsIndex = 3;
            buyButton.IsEnabled = true;
        }

        private void goods4_MouseEnter(object sender, MouseEventArgs e)
        {
            goodsName.Text = UserData.goodsData.Rows[4]["goodsName"].ToString();
            goodsSpecification.Text = UserData.goodsData.Rows[4]["goodsSpecification"].ToString();
            goodsPrice.Text = UserData.goodsData.Rows[4]["goodsPrice"].ToString();
            storeName.Text = UserData.goodsData.Rows[4]["storeName"].ToString();
            goodsInventory.Text = UserData.goodsData.Rows[4]["goodsInventory"].ToString();

            nowGoodsIndex = 4;
            buyButton.IsEnabled = true;
        }

        private void reverseButton_Click(object sender, RoutedEventArgs e)
        {
            if (priceSort == 1)
            {
                priceSort = 2;
                (sender as Button).Content = "价格降序";
            }
            else
            {
                priceSort = 1;
                (sender as Button).Content = "价格升序";
            }
            searchGoodsButton_Click(sender, e);
        }

        private void nextPageButton_Click(object sender, RoutedEventArgs e)
        {
            page++;
            searchGoodsButton_Click(sender, e);

            goodsName.Text = "";
            goodsSpecification.Text = "";
            goodsPrice.Text = "";
            storeName.Text = "";
            goodsInventory.Text = "";
        }

        private void priorPageButton_Click(object sender, RoutedEventArgs e)
        {
            if (page > 1)
                page--;
            searchGoodsButton_Click(sender, e);


            goodsName.Text = "";
            goodsSpecification.Text = "";
            goodsPrice.Text = "";
            storeName.Text = "";
            goodsInventory.Text = "";
        }

        private void buyButton_Click(object sender, RoutedEventArgs e)
        {
            ChooseDeliveryAddressWindow chooseDeliveryAddressWindow = new();
            chooseDeliveryAddressWindow.nowGoodsIndex = nowGoodsIndex;
            chooseDeliveryAddressWindow.buyCount = buyCount.Text;
            chooseDeliveryAddressWindow.Show();
        }
    }
}
